package ssn.common.view;

import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import egovframework.rte.fdl.string.EgovStringUtil;

public class CommonExcelView extends AbstractExcelView {

	@SuppressWarnings("rawtypes")
	protected void buildExcelDocument(Map model, HSSFWorkbook wb,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {

		Map map = (Map) model.get("excelMap");

		String sheetName = (String)map.get("sheetName");

		// 시트생성
		HSSFSheet sheet = wb.createSheet(EgovStringUtil.null2string(sheetName, "default"));
		sheet.setColumnWidth(0, 4000);

		// 폰트 (헤더)
		HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 10);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 폰트 (본문)
		HSSFFont font2 = wb.createFont();
		font2.setFontHeightInPoints((short) 10);
		// 헤더 스타일
		HSSFCellStyle style = wb.createCellStyle();
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBottomBorderColor(HSSFColor.BLACK.index);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setLeftBorderColor(HSSFColor.BLACK.index);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setRightBorderColor(HSSFColor.BLACK.index);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setTopBorderColor(HSSFColor.BLACK.index);
		style.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setFont(font);
		// 본문 스타일
		HSSFCellStyle style2 = wb.createCellStyle();
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBottomBorderColor(HSSFColor.BLACK.index);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setLeftBorderColor(HSSFColor.BLACK.index);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setRightBorderColor(HSSFColor.BLACK.index);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style2.setTopBorderColor(HSSFColor.BLACK.index);
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setFont(font2);

		// ROW/CELL
		HSSFRow row    = null;	// 본문 ROW
		HSSFCell cell  = null;	// 본문 CELL

		List result = (List) map.get("excelList");
		Map headerName = (Map)map.get("headerName");
		int cellIndex = 0;

		// 헤더 설정
		row = sheet.createRow(0);

		if(headerName != null){
			Iterator iterator = headerName.keySet().iterator();
			while(iterator.hasNext()){
				String keyName = (String)iterator.next();

				cell = row.createCell(cellIndex);
				cell.setCellStyle(style);
				cell.setCellValue((String)headerName.get(keyName));
				cellIndex++;
			}
		}

		// 내용 세팅
		for (int i = 0, size = result.size(); size > i; i++) {
			Map excelMap = (Map) result.get(i);
			int j = 0;
			row = sheet.createRow(i + 1);

			Iterator iterator = headerName.keySet().iterator();
			while(iterator.hasNext()){
				String keyName = (String)iterator.next();
				cell = row.createCell(j);
				cell.setCellStyle(style2);
				if(excelMap.get(keyName) instanceof String){
					cell.setCellValue((String)excelMap.get(keyName));
				}else if(excelMap.get(keyName) instanceof Double){
					cell.setCellValue(String.valueOf(excelMap.get(keyName)));
				}
				j++;
			}
		}

		// HttpServletResponse 헤더 설정
        String userAgent = req.getHeader("User-Agent");
        String fileName  = URLEncoder.encode((String) map.get("fileName"), "UTF-8");

        if (userAgent.indexOf("MSIE 5.5") >= 0) {
        	resp.setContentType("doesn/matter");
        	resp.setHeader("Content-Disposition","filename=\"" + fileName + "\"");
        } else {
        	resp.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
        }

	}
}